package cn.jhc.sevlet;//已经报废的老师分学期查找成绩Servlet

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.jhc.bean.Score;
import cn.jhc.bean.Student;
import cn.jhc.bean.Study;
import cn.jhc.bean.Teacher;
import cn.jhc.dao.ScoreDao;
import cn.jhc.dao.StudentDao;
import cn.jhc.dao.StudyDao;
import cn.jhc.dao.TeacherDao;
import cn.jhc.dao.impl.ScoreDaoImp;
import cn.jhc.dao.impl.StudentDaoImpl;
import cn.jhc.dao.impl.StudyDaoImpl;
import cn.jhc.dao.impl.TeacherDaoImpl;

/**
 * Servlet implementation class ScoreSelectServlet
 */
@WebServlet("/user/teacher/scoreselect.do")
public class ScoreSelectServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ScoreSelectServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String spage = request.getParameter("page");
		int page = 1;
		try {
			page = Integer.valueOf(spage);
		} catch (NumberFormatException e) {
			page = 1;
		}
		String username = request.getParameter("username");
		ScoreDao scoredao = new ScoreDaoImp();
		List<Score> scoress = scoredao.list();
		Set<Score> scores = new HashSet<Score>();
		for(Score score : scoress){
			if(score.getStudent().getSno().equals(username)){
				scores.add(score);
			}
		}
		long maxpage = scores.size()/ 10+ 1;
		/*StudyDao studyDao = new StudyDaoImpl();//任课老师
		TeacherDao teacherDao = new TeacherDaoImpl();	
		List<Study> studies = studyDao.list();	
		List<Teacher> teachers = new ArrayList(); 
		
		for(Score score : scores){
			for(Study study : studies){
				if(study.getCourse().getCoursename().equals(score.getCourse().getCoursename())
				&& study.getClazz().getName().equals(score.getStudent().getClazz().getName())){
					Teacher teacher = (Teacher) teacherDao.get(study.getTeacher().getTid());
					
					teachers.add(teacher);
				}
			}
		}
		request.setAttribute("teachers", teachers);*/
		request.setAttribute("page", page);
		request.setAttribute("scores", scores);
		request.setAttribute("maxpage", maxpage);
		request.getRequestDispatcher("tscore.jsp").forward(request,
				response);
	}

}
